Page({
    data: {
        categories: [
            {
                id: 1,
                name: '推荐分类',
                products: [
                    { id: 1, name: '狗粮 优质', price: '17.95', pricenow: '3.95', image: '/image/banner/banner1.jpg' },
                    { id: 2, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 3, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 4, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 5, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 6, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 7, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 8, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' },
                    { id: 9, name: '猫粮 优质', price: '20.50', pricenow: '3.95', image: '/image/banner/banner2.jpg' }
                ]
            },
            {
                id: 2,
                name: '宠物日用',
                products: [
                    { id: 10, name: '宠物梳子', price: '10.00', pricenow: '5.00', image: '/image/banner/banner3.jpg' },
                    { id: 11, name: '宠物垫子', price: '15.00', pricenow: '8.00', image: '/image/banner/banner4.jpg' }
                ]
            },
            {
                id: 3,
                name: '宠物服装',
                products: [
                    { id: 12, name: '宠物T恤', price: '25.00', pricenow: '15.00', image: '/image/banner/banner5.jpg' },
                    { id: 13, name: '宠物帽子', price: '15.00', pricenow: '10.00', image: '/image/banner/banner6.jpg' }
                ]
            },
            {
                id: 4,
                name: '宠物主粮',
                products: [
                    { id: 14, name: '狗狗干粮', price: '30.00', pricenow: '20.00', image: '/image/banner/banner7.jpg' },
                    { id: 15, name: '猫咪湿粮', price: '25.00', pricenow: '18.00', image: '/image/banner/banner8.jpg' }
                ]
            },
            {
                id: 5,
                name: '宠物零食',
                products: [
                    { id: 16, name: '狗狗饼干', price: '10.00', pricenow: '6.00', image: '/image/banner/banner9.jpg' },
                    { id: 17, name: '猫咪罐头', price: '15.00', pricenow: '10.00', image: '/image/banner/banner10.jpg' }
                ]
            },
            {
                id: 6,
                name: '宠物出行',
                products: [
                    { id: 18, name: '宠物牵引绳', price: '20.00', pricenow: '15.00', image: '/image/banner/banner11.jpg' },
                    { id: 19, name: '宠物背包', price: '30.00', pricenow: '25.00', image: '/image/banner/banner12.jpg' }
                ]
            },
            {
                id: 7,
                name: '宠物玩具',
                products: [
                    { id: 20, name: '狗狗飞盘', price: '25.00', pricenow: '20.00', image: '/image/banner/banner13.jpg' },
                    { id: 21, name: '猫咪抓板', price: '20.00', pricenow: '15.00', image: '/image/banner/banner14.jpg' }
                ]
            },
            {
                id: 8,
                name: '宠物医疗',
                products: [
                    { id: 22, name: '宠物药品', price: '50.00', pricenow: '40.00', image: '/image/banner/banner15.jpg' },
                    { id: 23, name: '宠物口罩', price: '15.00', pricenow: '10.00', image: '/image/banner/banner16.jpg' }
                ]
            },
            {
                id: 9,
                name: '安全自驾',
                products: [
                    { id: 24, name: '车载宠物座椅', price: '100.00', pricenow: '80.00', image: '/image/banner/banner17.jpg' },
                    { id: 25, name: '宠物旅行箱', price: '60.00', pricenow: '50.00', image: '/image/banner/banner18.jpg' }
                ]
            }
        ],
        tabs: [
            { id: 0, name: '特价' },
            { id: 1, name: '狗类' },
            { id: 2, name: '猫类' },
            { id: 3, name: '兔子' },
            { id: 4, name: '兔子' },
            { id: 5, name: '兔子' },
            { id: 6, name: '兔子' },
            { id: 7, name: '兔子' },
            { id: 8, name: '兔子' },
            { id: 9, name: '兔子' }
        ],
        currentCategoryIndex: 0,
        currentTabIndex: 0,
        visibleCount: 4,
        currentProducts: [],
        showBottomTip: false,
        showMoreTabs: false,
        searchKeyword: ''
    },

    onLoad() {
        this.setData({
            currentProducts: this.data.categories[0].products || [],
            visibleTabs: this.data.tabs.slice(0, this.data.visibleCount),
            hiddenTabs: this.data.tabs.slice(this.data.visibleCount)
        });
        this.performSearch(); // 初始化时执行一次搜索以应用默认过滤条件
    },

    changeCategory(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            currentCategoryIndex: index,
            currentProducts: this.data.categories[index].products || [],
            showBottomTip: false
        }, () => {
            this.performSearch();
        });
    },

    changeTab(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({ currentTabIndex: index }, () => {
            this.performSearch();
        });
    },

    toggleMoreTabs() {
        this.setData({ showMoreTabs: !this.data.showMoreTabs });
    },

    onReachBottom() {
        this.setData({ showBottomTip: true });
    },

    addToCart(e) {
        const productId = e.currentTarget.dataset.productId;
        console.log('Added to cart:', productId);
        wx.showToast({
            title: '购物车功能开发中',
            icon: 'none'
          });
    },

    onSearchInput(e) {
        this.setData({ searchKeyword: e.detail.value }, () => {
            this.performSearch();
        });
    },

    performSearch() {
        const category = this.data.categories[this.data.currentCategoryIndex];
        let filteredProducts = category.products;

        if (this.data.currentTabIndex === 0) {
            // 特价标签，假设所有价格低于原价的商品为特价
            filteredProducts = filteredProducts.filter(product => parseFloat(product.pricenow) < parseFloat(product.price));
        } else {
            const selectedTabName = this.data.tabs[this.data.currentTabIndex].name.toLowerCase();
            filteredProducts = filteredProducts.filter(product => product.name.toLowerCase().includes(selectedTabName));
        }

        if (this.data.searchKeyword) {
            filteredProducts = filteredProducts.filter(product => product.name.toLowerCase().includes(this.data.searchKeyword.toLowerCase()));
        }

        this.setData({ currentProducts: filteredProducts });
    }
});